package com.atguigu.gulimall.seckill.scheduled;

import com.atguigu.gulimall.seckill.service.SecKillService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @Classname SecKillSkuScheduled
 * @Description 秒杀商品的定时商家任务
 *
 *          每天晚上3点，商家最近三天需要秒杀的商品
 *          当天：00:00:00 - 23:59:59
 *          明天：00:00:00 - 23:59:59
 *          后天：00:00:00 - 23:59:59
 * @Date 2024/8/2 21:45
 * @Created by 369502594
 */
@Slf4j
@Service
public class SecKillSkuScheduled {

    @Autowired
    SecKillService secKillService;

    @Autowired
    RedissonClient redissonClient;

    private final String opload_lock = "seckill:upload:lock";

    // TODO 幂等性处理
    @Scheduled(cron = "*/3 * * * * ?")
    public void uploadSecKillSkuLatest3Days() {
        // 1、重复上架无需处理
        log.info("上架秒杀的商品信息...");
        // 分布式锁
        RLock lock = redissonClient.getLock(opload_lock);
        lock.lock(10, TimeUnit.SECONDS);
        try{
            secKillService.uploadSecKillSkuLatest3Days();
        }finally {
            lock.unlock();
        }
    }
}
